package pl.register.android.widget;

/**
 * @author Mateusz Strzelecki
 * @since 2013-09-17
 */

import android.os.AsyncTask;
import android.widget.ProgressBar;

import java.util.Random;

public class FakeProgressThread extends AsyncTask<Void, Integer, Void> {

    private final ProgressBar progressBar;

    public FakeProgressThread(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    @Override
    protected Void doInBackground(Void... params) {
        downloadResources();
        return null;
    }

    private void downloadResources() {
        Random random = new Random();
        int count = 7;
        for (int i = 0; i <= count; i++) {
            int progress = (int) ((i / (float) count) * 100);
            publishProgress(progress);
            try {
                Thread.sleep(random.nextInt(300) + 100);
            } catch (InterruptedException ignore) {

            }
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        progressBar.setProgress(values[0]);
    }

}